/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pclong.config;

import com.pclong.config.entity.ServerConfigEntity;
import com.pclong.config.entity.TankAgentConfigEntity;
import java.io.File;



import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;

import org.apache.commons.io.monitor.FileAlterationObserver;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

/**
 *  
 * @author long@7road.com
 */
public class FileListener extends FileAlterationListenerAdaptor {

    @Override
    public void onDirectoryChange(File directory) {

        System.out.println("文件目录变更了:" + directory.getAbsolutePath());

    }

    @Override
    public void onDirectoryCreate(File directory) {

        System.out.println("文件目录创建了:" + directory.getAbsolutePath());

    }

    @Override
    public void onDirectoryDelete(File directory) {

        System.out.println("文件目录删除了:" + directory.getAbsolutePath());

    }

    @Override
    public void onFileChange(File file) {
//       if(file.getAbsolutePath().equals("E:\\temp\\1\\ServerConfigEntity.xml"))
//       {
//            Serializer serializer = new Persister();
//            try {             
//                ServerConfigEntity ser=serializer.read(ServerConfigEntity.class, file);
//                TankAgentConfigEntity.getInstance().server=ser;
//            } catch (Exception ex) {
//                Logger.getLogger(FileListener.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            System.out.println(TankAgentConfigEntity.getInstance().server.Id);
//       }
        if (file.getName().endsWith(".xml")) {
            String filename = file.getName().substring(0, file.getName().lastIndexOf("."));
            System.out.println(filename);

            try {

                Class cls = TankAgentConfigEntity.getInstance().getClass();                
                Field field = cls.getDeclaredField(filename);
                if (field != null) {
                    Object tObject=cls.newInstance();  
                    Serializer serializer = new Persister();
                    tObject=serializer.read(field.getType(), file);                
                    field.set(TankAgentConfigEntity.getInstance(),tObject );
                    System.out.println(TankAgentConfigEntity.getInstance().server.Id);
                }
           
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(FileListener.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(FileListener.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(FileListener.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        System.out.println("文件变更了:" + file.getAbsolutePath());

    }

    @Override
    public void onFileCreate(File file) {

        System.out.println("文件创建了:" + file.getAbsolutePath());

    }

    @Override
    public void onFileDelete(File file) {

        System.out.println("文件删除了:" + file.getAbsolutePath());

    }

    @Override
    public void onStart(FileAlterationObserver observer) {

        System.out.println("开始监听:" + observer.getDirectory());

    }

    @Override
    public void onStop(FileAlterationObserver observer) {

        System.out.println("停止监听:" + observer.getDirectory());

    }
}
